supernova: fix for small audio vector sizes
[supercollider.git] / examples / demonstrations / 100 FM Synths.scd
blob9ab83e4269875f1bd803fb3eb107befbe0406ee2
2 // James McCartney
5 s.boot;
8 // define a simple reverb.
9 SynthDef("reverb", {
10         var in;
11         in = In.ar(0, 2);
12         5.do({ in = AllpassN.ar(in, 0.05, [0.05.rand, 0.05.rand], 2); });
13         ReplaceOut.ar(0, in);
14 }).add;
19 // define 100 randomly generated percussive FM instruments.
20 // this will take a few seconds.
21 // this code can be modified to generate sustaining instruments.
22 var carrierF, middleF, modulatorF;
23 var maxAttack = 0.4;
25 carrierF = { arg freq=440, mod=0, mix=0, gate=1;
26         var e, m;
27         //e = Env.adsr(exprand(0.001,maxAttack), linrand(3.0), rrand(0.4,1.0).squared, rrand(0.001,0.2));
28         e = Env.perc(exprand(0.001,maxAttack), exprand(0.1,2.0));
29         m = linrand(10) + 1;
30         e = EnvGen.kr(e, gate,  rrand(0.5,0.6).rand.squared);
31         SinOsc.ar(freq * m, mod, e, mix);
34 middleF = { arg freq=440, mod=0, mix=0, gate=1;
35         var e, m;
36         //e = Env.adsr(exprand(0.001,maxAttack), linrand(3.0), 1.0.rand.squared, rrand(0.001,0.2));
37         e = Env.perc(exprand(0.001,maxAttack), exprand(0.1,2.0));
38         m = linrand(5) + 1;
39         e = EnvGen.kr(e, gate,  3.0.rand.squared);
40         SinOsc.ar(freq * m, mod, e, mix);
43 modulatorF = { arg freq=440, mix=0, gate=1;
44         var e, m;
45         //e = Env.adsr(exprand(0.001,maxAttack), linrand(3.0), 1.0.rand.squared, rrand(0.001,0.2));
46         e = Env.perc(exprand(0.001,maxAttack), exprand(0.1,2.0));
47         m = linrand(5) + 1;
48         e = EnvGen.kr(e, gate,  3.0.rand.squared);
49         SinOsc.ar(freq * m, 1.3.rand.cubed, e, mix);
52 100.do { |i|
53         var name;
54         name = "fmgen_a_" ++ i;
55         SynthDef(name, { arg freq=440, amp=1, gate=1, pan=0;
56                 var c, m;
57                                         
58                 c = 0;
59                 [
60                         {
61                                 // sum of 3 modulator->carrier pairs
62                                 [\A, i].postln;
63                                 3.do {
64                                         var f;
65                                         f = freq + 1.8.rand2.squared;
66                                         m = modulatorF.(f, 0, gate);
67                                         c = carrierF.(f, m, c, gate);
68                                 }
69                         },
70                         {
71                                 // sum of 2 modulator->modulator->carrier chains
72                                 [\B, i].postln;
73                                 2.do {
74                                         var f, m;
75                                         f = freq + 1.8.rand2.squared;
76                                         m = modulatorF.(f, 0, gate);
77                                         m = middleF.(f, m, 0, gate);
78                                         c = carrierF.(f, m, c, gate);
79                                 }
80                         },
81                         {
82                                 // sum of 2 modulator-+->carrier
83                                 //                    |
84                                 //                    +->carrier
85                                 [\C, i].postln;
86                                 2.do {
87                                         var f;
88                                         f = freq + 1.8.rand2.squared;
89                                         m = modulatorF.(f, 0, gate);
90                                         c = carrierF.(f, m, c, gate);
91                                         c = carrierF.(f, m, c, gate);
92                                 }
93                         },
94                 ].choose.value;
95                 
96                 DetectSilence.ar(c, doneAction: 2);
97                 Out.ar(0, Pan2.ar(c, pan, amp));
98         }).add;
104 // listen randomly to the random FM instruments.
105 Routine({
106         var name;       
107         s.sendMsg(\s_new, \reverb, 1000, 1, 0);
108         name = "fmgen_a_" ++ 100.rand;
109         name.postln;
110         1000.do {       
111                 var freq, id;
112                 if (0.08.coin) { name = "fmgen_a_" ++ 100.rand; name.postln };
113                 [1,1,1,1,1,2,2].choose.do {
114                         freq = rrand(24,84).midicps;
115                         id = 1000000000.rand;
116                         s.sendMsg(\s_new, name, id, 0, 0, \freq, freq, \amp, 0.4, \pan, 1.0.rand2);
117                 };
118                 [0.1, 0.1, 0.1, 0.2, 0.2, 0.4, 0.8, 1.6].choose.wait;
119         };
120         s.sendMsg(\n_free, 1000);
121 }).play;